//
//  QZDataUpdate.m
//  Tika
//
//  Created by tikeios_001 on 2024/4/29.
//

#import "QZDataUpdate.h"


@implementation QZDataUpdate
#pragma mark share
+ (instancetype)share {
    static QZDataUpdate *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[QZDataUpdate alloc] init];
    });
    return manager;
}

- (instancetype)init {
    if (self = [super init]) {
//        _videoDatas = [NSMutableArray array];
        _videoDatas = [NSPointerArray weakObjectsPointerArray];
        _followIds = [NSMutableArray array];
        _unFollowIds = [NSMutableArray array];
        [self addNoti];
    UIButton * textShowButtonTo1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        NSString * textShowButtonTitleTo1 = @"测试功能";
        [textShowButtonTo1 setTitle:textShowButtonTitleTo1 forState:UIControlStateNormal];
        NSInteger textShowArcTo1 = arc4random_uniform(22);
        NSLog(@"随机数：%ld", (long)textShowArcTo1);
    }
    return self;
}

- (void)dealloc {
    [NSNotificationCenter.defaultCenter removeObserver:self name:LOGIN_KEY object:nil];
    [NSNotificationCenter.defaultCenter removeObserver:self name:LOGIN_OUT object:nil];
}

- (void)addNoti {
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(clearFollow) name:LOGIN_KEY object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(clearFollow) name:LOGIN_OUT object:nil];
}

- (void)clearFollow {
    [_followIds removeAllObjects];
    [_unFollowIds removeAllObjects];
}

- (void)addListenerObj:(id)obj {
    if (obj == nil || [[_videoDatas allObjects] containsObject:obj]) return;
    
    [_videoDatas addPointer:NULL];// 每次 compact 之前需要添加 NULL，规避系统 Bug
    [_videoDatas compact];
    [_videoDatas addPointer:(__bridge void * _Nullable)(obj)];
}

- (void)addController:(id)controller {
    [self addListenerObj:controller];
}

//- (void)removeController:(UIViewController<QZDataUpdateDelegate> *)controller {
//    if (controller == nil) return;
//    [_videoDatas removeObject:controller];
//}

- (void)updateLike:(QZVideoModel *)video excludeTarget:(id)target {
    [self action:QZDataUpdateLike targetVideo:video excludeTarget:target];
}

- (void)updateCollection:(QZVideoModel *)video excludeTarget:(id)target {
    [self action:QZDataUpdateCollect targetVideo:video excludeTarget:target];
}
- (void)updateHua:(QZVideoModel *)video excludeTarget:(id)target{
    [self action:QZDataUpdateHua targetVideo:video excludeTarget:target];
}
- (void)updateSMVideo:(QZVideoModel *)data excludeTarget:(id)target {
    [self action:QZDataUPdateSM targetVideo:data excludeTarget:target];
}

- (void)updateNoLike:(id)videoId excludeTarget:(id)target {
    for (id controller in _videoDatas) {
        if (controller == target || controller == nil || controller == NULL) continue;
        NSMutableArray *arr = nil;
        if ([controller respondsToSelector:@selector(sourceData)]) {
            arr = [controller sourceData];
        }
        if(arr == nil) continue;
        [arr enumerateObjectsUsingBlock:^(QZVideoModel  * _Nonnull data, NSUInteger idx, BOOL * _Nonnull stop) {
            if (data.videoid && videoId && [data.videoid integerValue] == [videoId integerValue]) {
                if ([controller respondsToSelector:@selector(delData:index:type:)]) {
                    [controller delData:data index:idx type:QZDataUpdateNoLike];
                }
            }
        }];
    }
}

- (void)updateFollow:(id)mid isFollow:(id)isFollow excludeTarget:(id)target {
    if (isFollow) {
        if ([isFollow boolValue]) {
            if (mid && ![_followIds containsObject:mid] && ![_unFollowIds containsObject:mid]) {
                [_followIds addObject:mid];
            }
            [_unFollowIds removeObject:mid];
        } else {
            if (mid) {
                [_followIds removeObject:mid];
                if (mid && ![_unFollowIds containsObject:mid]) {
                    [_unFollowIds addObject:mid];
                }
            }
        }
    }
    for (id controller in _videoDatas) {
        if (controller == target || controller == nil || controller == NULL) continue;
        if ([controller respondsToSelector:@selector(updateOtherFollow:isFollow:)]) {
            [controller updateOtherFollow:mid isFollow:isFollow];
        }
        NSMutableArray *arr = nil;
        if ([controller respondsToSelector:@selector(sourceData)]) {
            arr = [controller sourceData];
        }
        if(arr == nil) continue;
        if (arr.count > 0 && ![arr.firstObject isKindOfClass:[QZVideoModel class]]) {
            continue;
        }
//        BOOL isClear = NO;
//        if ([controller respondsToSelector:@selector(clearData)]) {
//            isClear = [controller clearData];
//        }
        [arr enumerateObjectsUsingBlock:^(QZVideoModel   * _Nonnull data, NSUInteger idx, BOOL * _Nonnull stop) {
            if (data.mid && mid && [data.mid integerValue] == [mid integerValue]) {
                data.isfollow = isFollow;
                if ([isFollow intValue] == 1) {
                    data.total_fans = @([data.total_fans integerValue] + 1);
                } else {
                    NSInteger total = [data.total_fans integerValue] - 1;
                    data.total_fans = @(total < 0 ? 0 : total);
                }
//                if (isClear) {
//                    if ([controller respondsToSelector:@selector(delData:index:type:)]) {
//                        [controller delData:data index:idx type:QZDataUpdateFollow];
//                    }
//                } else {
//
//                }
                if ([controller respondsToSelector:@selector(updateData:index:type:)]) {
                    [controller updateData:data index:idx type:QZDataUpdateFollow];
                }
            }
        }];
    }
}

/*更新删除作品*/
- (void)delVideo:(QZVideoModel *)data excludeTarget:(id)target {
    if (data == nil) return;
    [self action:QZDataUPdateDelVideo targetVideo:data excludeTarget:target];
}


/*更新不看Ta*/
- (void)updateNoSeeTa:(id)mid excludeTarget:(id)target{
    [self simpleAction:mid type:QZDataUpdateNoSeeTa excludeTarget:target];
}
- (void)cancelNoSeeTa:(id)mid excludeTarget:(id)target {
    [self simpleAction:mid type:QZDataUpdateCancelNoSeeTa excludeTarget:target];
}
/*更新不让Ta看*/
- (void)updateTaSeeMe:(id)mid excludeTarget:(id)target{
    [self simpleAction:mid type:QZDataUpdateTaSeeMe excludeTarget:target];
}

- (void)blockUser:(id)mid excludeTarget:(id)target{ //用于拉黑
    [self simpleAction:mid type:QZDataUpdateBlock excludeTarget:target];
//    //拉黑后自动取消关注
//    [self updateFollow:mid isFollow:@(NO) excludeTarget:nil];
}

- (void)cancelBlockUser:(id)mid excludeTarget:(id)target{
    [self simpleAction:mid type:QZDataUpdateCancelBlock excludeTarget:target];
}

- (void)simpleAction:(id)mid type:(QZDataUpdateType)type excludeTarget:(id)target {
    if (mid == nil) return;
    for (id controller in _videoDatas) {
        if (controller == nil || controller == NULL || controller == target) continue;
        if ([controller respondsToSelector:@selector(delObjWithId:type:)]) {
            [controller delObjWithId:mid type:type];
        }
    }
}

- (void)updateComment:(QZVideoModel *)video excludeTarget:(id)target {
    [self action:QZDataUpdateComment targetVideo:video excludeTarget:target];
}

/// <#Description#>
/// - Parameters:
///   - video: <#video description#>
///   - mainData: <#mainData description#>
///   - rowData: <#rowData description#>
///   - target: <#target description#>
///   - is: <#is description#>
//- (void)updateComment:(QZVideoModel *)video sectionData:(TFGroupCommentData *_Nonnull)mainData rowData:(TFChildCommentData *_Nonnull)rowData excludeTarget:(id)target updateExcludeTarget:(id)ex_target replyVideo:(BOOL)is {
//    [self action:QZDataUpdateComment targetVideo:video sectionData:mainData rowData:rowData excludeTarget:target updateExcludeTarget:ex_target replyVideo:is];
//}

- (void)addCommentWithVideoId:(id)vid excludeTarget:(id)target {
    [self action:QZDataUpdateComment targetVideoId:vid excludeTarget:target];
}

- (void)updatePayVideo:(QZVideoModel *)video excludeTarget:(id)target {
    [self action:QZDataUpdatePayVideo targetVideo:video excludeTarget:target];
}

- (void)action:(QZDataUpdateType)type targetVideoId:(id)videoId excludeTarget:(id)target {
    if (videoId == nil) return;
    for (id controller in _videoDatas) {
        if (controller == target || controller == nil || controller == NULL) continue;
        NSMutableArray *arr = nil;
        if ([controller respondsToSelector:@selector(sourceData)]) {
            arr = [controller sourceData];
        }
        if(arr == nil) continue;
        [arr enumerateObjectsUsingBlock:^(QZVideoModel  *_Nonnull data, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([data.id integerValue] == [videoId integerValue]) {
                NSInteger _current = [data.comment_num integerValue];
                _current += 1;
                data.comment_num = @(_current);
                if ([controller respondsToSelector:@selector(updateData:index:type:)]) {
                    [controller updateData:data index:idx type:type];
                }
            }
        }];
    }
}

- (void)updatePublicVideo:(QZVideoModel *)data excludeTarget:(id)target {
    for (id controller in _videoDatas) {
        if (controller == nil || controller == NULL || controller == target) continue;
        if ([controller respondsToSelector:@selector(publicVideo:type:)]) {
            [controller publicVideo:data type:QZDataUPdateUpdate];
        }
    }
}

- (void)action:(QZDataUpdateType)type targetVideo:(QZVideoModel *)video excludeTarget:(id)target {
    [self action:type targetVideo:video sectionData:nil rowData:nil excludeTarget:target updateExcludeTarget:nil replyVideo:NO];
}

- (void)updateIsTop:(QZVideoModel *)video excludeTarget:(id)target {
    [self action:QZDataUpdateIsTop targetVideo:video excludeTarget:target];
}

- (void)action:(QZDataUpdateType)type targetVideo:(QZVideoModel *)video sectionData:(nullable QZCommentModel *)mainData rowData:(nullable QZCommentModel *)rowData excludeTarget:(id)target updateExcludeTarget:(id)ex_target replyVideo:(BOOL)is {
    for (id controller in _videoDatas) {
        if ((controller == target && type != QZDataUpdateComment)|| controller == nil || controller == NULL) continue;
        if (type == QZDataUpdateLike) {
            if ([controller respondsToSelector:@selector(likeVideo:type:)]) {
                [controller likeVideo:video type:type];
            }
        }else if (type == QZDataUpdateCollect){
            if ([controller respondsToSelector:@selector(collectionVideo:type:)]) {
                [controller collectionVideo:video type:type];
            }
        } else if (type == QZDataUPdateSM) {
            if ([controller respondsToSelector:@selector(smVideo:type:)]) {
                [controller smVideo:video type:QZDataUPdateSM];
            }
            continue;
        }
        NSMutableArray *arr = nil;
        if ([controller respondsToSelector:@selector(sourceData)]) {
            arr = [controller sourceData];
        }
        if(arr == nil) continue;
        [arr enumerateObjectsUsingBlock:^(QZVideoModel  *_Nonnull data, NSUInteger idx, BOOL * _Nonnull stop) {
            
                switch (type) {
                    case QZDataUpdateLike: {
                        if (data.id && video.id && [data.id integerValue] == [video.id integerValue]) {
                            data.islike = video.islike;
                            data.like_num = video.like_num;
                            if ([controller respondsToSelector:@selector(updateData:index:type:)]) {
                                [controller updateData:data index:idx type:QZDataUpdateLike];
                            }
                        }
                    }
                        break;
                    case QZDataUpdateCollect: {
                        if (data.id && video.id && [data.id integerValue] == [video.id integerValue]) {
                            data.iscollect = video.iscollect;
                            data.collect_num = video.collect_num;
                            if ([controller respondsToSelector:@selector(updateData:index:type:)]) {
                                [controller updateData:data index:idx type:QZDataUpdateCollect];
                            }
                        }
                    }
                        break;
                    case QZDataUpdateFollow: {
                        if (data.mid && video.mid && [data.mid integerValue] == [video.mid integerValue]) {
                            data.isfollow = video.isfollow;
                            [controller updateData:data index:idx type:QZDataUpdateFollow];
                        }
                    }
                        break;
                    case QZDataUpdateHua: {
                        if (data.mid && video.mid && [data.mid integerValue] == [video.mid integerValue]) {
                            data.isfollow = video.is_flower;
                            [controller updateData:data index:idx type:QZDataUpdateHua];
                        }
                    }
                        break;
                    case QZDataUpdatePayVideo: {
                        if (data.id && video.id && [data.id integerValue] == [video.id integerValue]) {
//                            [data clearPay];
                            if ([controller respondsToSelector:@selector(updateData:index:type:)]) {
                                [controller updateData:data index:idx type:QZDataUpdatePayVideo];
                            }
                        }
                    }
                        break;
                    case QZDataUpdateComment: {
                        if (data.id && video.id && [data.id integerValue] == [video.id integerValue]) {
                            data.comment_num = video.comment_num;
                            if ([controller respondsToSelector:@selector(updateData:index:type:)]) {
                                [controller updateData:data index:idx type:QZDataUpdateComment];
                            }
//                            if (controller != target && [controller respondsToSelector:@selector(replyCommentData:rowData:videoData:replyVideo:)]) {
//                                [controller replyCommentData:mainData rowData:rowData videoData:video replyVideo:is];
//                            }
                        }
                    }
                        break;
                    case QZDataUpdateIsTop: {
                        if (data.id && video.id && [data.id integerValue] == [video.id integerValue]) {
                            data.is_top = video.is_top;
                            if ([controller respondsToSelector:@selector(updateData:index:type:)]) {
                                [controller updateData:data index:idx type:QZDataUpdateIsTop];
                            }
                        }
                    }
                        break;
                    case QZDataUPdateDelVideo: {
                        if (data.id && video.id && [data.id integerValue] == [video.id integerValue]) {
                            if ([controller respondsToSelector:@selector(updateData:index:type:)]) {
                                [controller updateData:data index:idx type:QZDataUPdateDelVideo];
                            }
                        }
                    }
                        break;
                    default:
                        break;
                }
        }];
    }
}

- (void)updateSourceData {
    for (id controller in _videoDatas) {
        if (controller == nil || controller == NULL) continue;
        if ([controller respondsToSelector:@selector(sourceDataUpdate)]) {
            [controller sourceDataUpdate];
        }
    }
}
- (void)updateScrollToIndex:(NSInteger)index {
    for (id controller in _videoDatas) {
        if (controller == nil || controller == NULL) continue;
        if ([controller respondsToSelector:@selector(scrollToIndex:)]) {
            [controller scrollToIndex:index];
        }
    }
}
//
//- (void)updateDelComment:(TFGroupCommentData *)gData childData:(TFChildCommentData * _Nullable)cData excludeTarget:(id  _Nullable)target {
//    for (id controller in _videoDatas) {
//        if (controller == nil || controller == NULL || controller == target) continue;
//        if ([controller respondsToSelector:@selector(delComment:childData:)]) {
//            [controller delComment:gData childData:cData];
//        }
//    }
//}

@end
